﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp30
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Hotkey hotkey;
            hotkey = new Hotkey(this.Handle);
            Hotkey.Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F3, Hotkey.KeyFlags.MOD_ALT); //定义快键(Alt + F3) ,如果是hotkey.RegisterHotkey(System.Windows.Forms.Keys.F3, Hotkey.KeyFlags.MOD_NONE);//只注册F3
            hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);

        }
        public void OnHotkey(int HotkeyID) //设置热键的行为
        {
            if (HotkeyID == Hotkey.Hotkey1)
            {
                if (this.Visible == true)
                    this.Visible = false;
                else
                    this.Visible = true;
                // 设置窗体置顶
                this.TopMost = true;
            }
            else
            {
                this.Visible = false;
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        //屏蔽关闭按钮
        protected override void WndProc(ref Message m)
        {
            var WM_SYSCOMMAND = 0X112;
            var SC_CLOSE = 0XF060;
            if (m.Msg == WM_SYSCOMMAND && m.WParam == (IntPtr)SC_CLOSE)
            {
                // MessageBox.Show("X 被按下了");
                this.Visible = false;
                return;
            }
            base.WndProc(ref m);
        }

        private void Form1_Activated_1(object sender, EventArgs e)
        {
            this.Visible = false;
        }
    }
}
